package com.tiny.work.test.threadpool;

import java.util.concurrent.Semaphore;

public class ThreadPoolOutTest2 {

    private static Semaphore A = new Semaphore(1);
    private static Semaphore B = new Semaphore(1);
    private static Semaphore C = new Semaphore(1);

    public static void main(String args[]) throws InterruptedException {
        Thread t1 = new Thread(new RunnableTask(A,B,'A'));
        Thread t2 = new Thread(new RunnableTask(B,C,'B'));
        Thread t3 = new Thread(new RunnableTask(C,A,'C'));
        B.acquire();
        C.acquire();// 开始只有A可以获取, BC都不可以获取, 保证了A最先执行w
        t1.start();
        t2.start();
        t3.start();
    }

    public static class RunnableTask implements Runnable{

        Semaphore cur;
        Semaphore next;
        int capacity = 10;
        char data;

        @Override
        public void run() {
            for (int i=0; i< capacity; i++) {
                try {
                    cur.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(data);
                next.release();
            }
        }

        public RunnableTask(Semaphore cur, Semaphore next, char data) {
            this.cur = cur;
            this.next = next;
            this.data= data;
        }
    }
}


